﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.MO
{
    public partial class frm_MOOptionEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsMODAO clsMODAO = new ClsMODAO();
        DataTable dt;
        ClsMOOptionTable b = new ClsMOOptionTable();

        public string ID;
        public string mONo;
        public string partID;
        public string mOQty;
        public string deadline;
        public string machineID;
        public string optionID;
        public string lastOption;
        public string jigID;
        public string proTime;
        public string clampTime;
        public string finished;
        public string finishdate;
        public string shift;
        public string note;
        public string finishMachine; 
        public frm_MOOptionEdit()
        {
            InitializeComponent();
        }

        private void frm_MOOptionEdit_Load(object sender, EventArgs e)
        {
            //danh sách MONo
            DataTable dtMONo = new DataTable();
            dtMONo = clsMODAO.GetAllMO();
            DataRow rMONo = dtMONo.NewRow();
            rMONo["MONo"] = "";
            dtMONo.Rows.InsertAt(rMONo, 0);
            cbMONo.DataSource = dtMONo;
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";
            //List<ClsMOOptionTable> objs = new List<ClsMOOptionTable>();
            //ClsMOOptionTable obj1 = new ClsMOOptionTable();
            //obj1.MONo = "";
            //obj1.PartID = "";
            //objs.Add(obj1);
            //foreach (DataRow r in dtMONo.Rows)
            //{
            //    ClsMOOptionTable obj = new ClsMOOptionTable();
            //    obj.MONo = r["MONo"].ToString();
            //    obj.PartID = r["PartID"].ToString();
            //    objs.Add(obj);
            //}
            //cbMONo.DataSource = objs;
            //cbMONo.DisplayMember = "MONo";
            //cbMONo.ValueMember = "MONo";

            //danh sách PartID
            DataTable dtPartID = new DataTable();
            dtPartID = clsMODAO.GetAllPartID();
            DataRow rPartID = dtPartID.NewRow();
            rPartID["PartNo"] = "";
            //rPartID["PartID"] = "";
            dtPartID.Rows.InsertAt(rPartID, 0);
            cbPartID.DataSource = dtPartID;
            cbPartID.DisplayMember = "PartNo";
            cbPartID.ValueMember = "PartNo";


            //danh sách OptionID
            DataTable dtOptionID = new DataTable();
            dtOptionID = clsMODAO.GetAllOptionID();
            DataRow rOptionID = dtOptionID.NewRow();
            rOptionID["OptionID"] = "";
            //rPartID["PartID"] = "";
            dtOptionID.Rows.InsertAt(rOptionID, 0);
            cbOptionID.DataSource = dtOptionID;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //danh sách MachineID
            DataTable dtMachineID = new DataTable();
            dtMachineID = clsMODAO.GetAllMachineID();
            DataRow rMachineID = dtMachineID.NewRow();
            rMachineID["MachineID"] = "";
            //rPartID["PartID"] = "";
            dtMachineID.Rows.InsertAt(rMachineID, 0);
            cbMachineID.DataSource = dtMachineID;
            cbMachineID.DisplayMember = "MachineID";
            cbMachineID.ValueMember = "MachineID";

            //đưa giá trị vào danh sách
            cbMachineID.SelectedValue = machineID;
            cbMONo.SelectedValue = mONo;
            cbOptionID.SelectedValue = optionID;
            cbPartID.SelectedValue = partID;
            cbShift.SelectedText = shift;
            txtClampTime.Text = clampTime;
            txtJigType.Text = jigID;
            txtMOQty.Text = mOQty;
            txtNote.Text = note;
            txtProTime.Text = proTime;
            checkFinished.Checked = bool.Parse(finished);
            checkLastOption.Checked = bool.Parse(lastOption);
            dateDeadline.DateTime = DateTime.Parse(deadline);
            dateFinishDate.DateTime = DateTime.Parse(finishdate);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            b.RecordNo = int.Parse(ID);
            b.ClampTime = float.Parse(txtClampTime.Text);
            b.Deadline = dateDeadline.DateTime;
            b.Finishdate = dateFinishDate.DateTime;
            b.Finished = checkFinished.Checked;
            b.FinishMachine = "";
            b.JigID = txtJigType.Text;
            b.LastOption = checkLastOption.Checked;
            b.MachineID = cbMachineID.Text;
            b.MONo = cbMONo.Text;
            b.MOQty = int.Parse(txtMOQty.Text);
            b.Note = txtNote.Text;
            b.OptionID = cbOptionID.Text;
            b.PartID = cbPartID.Text;
            b.ProTime = float.Parse(txtProTime.Text);
            b.Shift = cbShift.Text;
            clsMODAO.UpdateMOOption(b);
            MessageBox.Show("Đã nhập xong");
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}